# -*- coding: utf-8 -*-
"""
Created on Sat Oct 16 17:14:19 2021

@author: zhuo木鸟

求解第2题前的数据预处理
"""

from curve_kmeans import Kmeans, draw_centroids
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import pickle

# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
plt.rcParams['font.size'] = 16

if __name__ == '__main__':
    # 读取数据集
    path = r'../datasets/附件2.xlsx'
    datasets = pd.read_excel(path, index_col=0)
    
    # 提取出原数据集中没有 op 的数据(待分类数据)
    datasets_without_op = datasets.loc[datasets['OP']!=datasets['OP']].copy()
    datasets.dropna(inplace=True)
    
    herbs_op = datasets['OP']
    datasets.drop(columns=['OP'], inplace=True)
    
    # 还有问题一的曲线，聚类方法对附件2的数据进行聚类
        
    
    # 提取数据中的中红外谱的波数
    wave_number = np.array(datasets.columns)
    pickle.dump(wave_number, open(r'../results/wave_number_2.pkl', 'wb'))
    
    # 进行聚类分析
    centroids, clusters = Kmeans(wave_number, datasets, k=9)
    draw_centroids(wave_number, centroids, clusters)
    
    
    # 使用PCI将违法将数据集降低到特定的维度，并求出该维度下其解释性方差比值
    cv_list = []
    n_list = range(1, 11)
    for n in n_list:
        pca = PCA(n_components=n)
        pca.fit(datasets)
        cv = np.sum(pca.explained_variance_ratio_)
        cv_list.append(cv)
    
    plt.bar(n_list, cv_list, width=1.0, edgecolor='black')
    plt.xlabel('目标维度')
    plt.ylabel('解释性方差比值')
    plt.title('不同 PCA 目标维度下的解释性方差比值')
    plt.grid()
    path = r'../pictures/附件2不同 PCA 目标维度下的解释性方差比值.png'
    plt.savefig(path)
    plt.show()
    
    
    pca = PCA(n_components=9)
    datasets_pca = pca.fit_transform(datasets)
    print('当 n=9 时解释性方差之比为：', np.sum(pca.explained_variance_ratio_))
    
    # 保存数据
    pickle.dump(datasets_pca, open(r'../results/datasets_2_pca.pkl', 'wb'))
    pickle.dump(herbs_op, open(r'../results/datasets_2_herbs_op.pkl', 'wb'))
    
    # 对没有 OP 的数据进行同样的 PCA 降维
    datasets_without_op_pca = pca.transform(datasets_without_op.iloc[:, 1:])
    pickle.dump(datasets_without_op_pca, open(r'../results/datasets_2_without_op_pca.pkl', 'wb'))
    pickle.dump(datasets_without_op, open(r'../results/datasets_2_without_op.pkl', 'wb'))
    
    
    
    
    

